package com.systop.dxjj.modules.student.controller;

import com.alibaba.fastjson.JSON;
import com.systop.dxjj.common.pojo.*;
import com.systop.dxjj.common.utils.JwtUtil;
import com.systop.dxjj.common.utils.R;
import com.systop.dxjj.common.utils.RedisCache;
import com.systop.dxjj.modules.student.service.StudentService;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.connector.Request;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@Api("学员管理模块")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @Autowired
    private RedisCache redisCache;

    StudentUser studentUser;
    /**
     * 学员登录
     */
    @PostMapping("/loginStudent")
    @ApiOperation(value = "学员登录",notes = "学员信息")
    public R loginStudent(@RequestBody Student student){
        return  studentService.loginStudent(student);
    }

    /**
     * 查询所有学员信息
     */
    @GetMapping("/selectAllStudents")
    @ApiOperation(value = "查询所有学员信息",notes = "学员信息")
    public R selectAllStudents(){

        return studentService.selectAllStudents();
    }

    /**
     *根据id查询学员信息
     */
    @GetMapping("/selectStudentById/{id}")
    @ApiOperation(value = "根据id查询学员信息",notes = "学员信息")
    public R  selectTeachById(@PathVariable Integer id){

        return studentService.selectStudentById(id);
    }

    /**
     *根据姓名查询学员信息
     */
    @GetMapping("/selectStudentByName/{name}")
    @ApiOperation(value = "根据学员姓名查询学员信息",notes = "学员信息")
    public R  selectTeachByName(@PathVariable String name){

        return studentService.selectStudentByName(name);
    }
    /**
     *根据姓名模糊查询学员信息
     */
    @GetMapping("/selectStudentByVagueName/{name}")
    @ApiOperation(value = "根据学员姓名模糊查询学员信息",notes = "学员信息")
    public R selectStudentByVagueName (@PathVariable String name){
        return studentService.selectStudentByVagueName(name);
    }

    /**
     *插入学员信息
     */
    @PostMapping("/insertStudent")
    @ApiOperation(value = "插入学员信息",notes = "学员信息")
    public R  insertTeach(@RequestBody Student student){

        return studentService.insertStudent(student);
    }


    /**
     *更新学员信息
     */
    @GetMapping("/updateStudent")
    @ApiOperation(value = "更新学员信息",notes = "学员信息")
    public R updateTeach(@RequestBody Student student){
        return studentService.updateStudent(student);
    }

    /**
     *删除学员信息
     */
    @GetMapping("/deleteStudent/{id}")
    @ApiOperation(value = "删除学员信息",notes = "学员信息")
    public R  deleteTeach(@PathVariable Integer id){
        studentService.deleteStudent(id);
        return R.ok("成功");
    }

    @PostMapping("/student/login")
    public ResponseResult login(@RequestBody Student student){
        //登录
        return studentService.studentLogin(student);
    }

    @RequestMapping("/student/logout")
    public ResponseResult logout(){
        return studentService.studentlogout();
    }

    @RequestMapping("/hello")
    public String hello( HttpServletRequest request){
        String token = request.getHeader("token");
        String stuid;
        try {
            Claims claims = JwtUtil.parseJWT(token);
            stuid = claims.getSubject();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("token非法");
        }
        String sturedisKey = "stulogin:" + stuid;
        Object cacheObject = redisCache.getCacheObject(sturedisKey);
        if (cacheObject instanceof StudentUser) {
            studentUser = (StudentUser) cacheObject;
        } else {
            studentUser = JSON.parseObject(JSON.toJSON(cacheObject).toString(), StudentUser.class);
        }

       return studentUser.getStudent().getStudentid().toString();

//        ValueOperations valueOperations = redisTemplate.opsForValue();
//        Object o = valueOperations.get("stulogin" + stuid);
//        return o.toString();

    }
}
